
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>5.2. async actions & flows · MobX 中文文档</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../refguide/object-api.html" />
    
    
    <link rel="prev" href="../refguide/action.html" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"SangKa","repo":"MobX-Docs-CN","type":"star","size":"small","count":true},{"user":"SangKa","repo":"MobX-Docs-CN","type":"fork","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    1. MobX 介绍
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../intro/overview.html">
            
                <a href="../intro/overview.html">
            
                    
                    1.1. MobX 要点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="../intro/concepts.html">
            
                <a href="../intro/concepts.html">
            
                    
                    1.2. 概念与原则
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../refguide/api.html">
            
                <a href="../refguide/api.html">
            
                    
                    2. API概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    3. observable 相关
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../refguide/observable.html">
            
                <a href="../refguide/observable.html">
            
                    
                    3.1. observable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../refguide/observable-decorator.html">
            
                <a href="../refguide/observable-decorator.html">
            
                    
                    3.2. @observable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../refguide/object.html">
            
                <a href="../refguide/object.html">
            
                    
                    3.3. objects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../refguide/array.html">
            
                <a href="../refguide/array.html">
            
                    
                    3.4. arrays
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../refguide/map.html">
            
                <a href="../refguide/map.html">
            
                    
                    3.5. maps
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../refguide/boxed.html">
            
                <a href="../refguide/boxed.html">
            
                    
                    3.6. boxed values
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../refguide/modifiers.html">
            
                <a href="../refguide/modifiers.html">
            
                    
                    3.7 decorators
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    4. 对 observables 作出响应
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../refguide/computed-decorator.html">
            
                <a href="../refguide/computed-decorator.html">
            
                    
                    4.1. (@)computed
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../refguide/autorun.html">
            
                <a href="../refguide/autorun.html">
            
                    
                    4.2. autorun
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../refguide/when.html">
            
                <a href="../refguide/when.html">
            
                    
                    4.3. when
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../refguide/reaction.html">
            
                <a href="../refguide/reaction.html">
            
                    
                    4.4. reaction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../refguide/observer-component.html">
            
                <a href="../refguide/observer-component.html">
            
                    
                    4.5. (@)observer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="react.html">
            
                <a href="react.html">
            
                    
                    4.6. 理解 MobX 对何作出响应
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    5. 改变 observables
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../refguide/action.html">
            
                <a href="../refguide/action.html">
            
                    
                    5.1. action
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.2" data-path="actions.html">
            
                <a href="actions.html">
            
                    
                    5.2. async actions & flows
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../refguide/object-api.html">
            
                <a href="../refguide/object-api.html">
            
                    
                    5.3. Object api
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    6. 工具函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../refguide/tojson.html">
            
                <a href="../refguide/tojson.html">
            
                    
                    6.1. toJS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../refguide/extend-observable.html">
            
                <a href="../refguide/extend-observable.html">
            
                    
                    6.2. extendObservable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../refguide/extending.html">
            
                <a href="../refguide/extending.html">
            
                    
                    6.3. createAtom
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../refguide/observe.html">
            
                <a href="../refguide/observe.html">
            
                    
                    6.4. intercept & observe
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#frompromise">
            
                    
                    6.5. mobxUtils.fromPromise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#fromresource">
            
                    
                    6.6. mobxUtils.fromResource
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#tostream">
            
                    
                    6.7. mobxUtils.toStream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#fromstream">
            
                    
                    6.8. mobxUtils.fromStream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#now">
            
                    
                    6.9. mobxUtils.now
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../refguide/create-transformer.html">
            
                <a href="../refguide/create-transformer.html">
            
                    
                    6.10. mobxUtils.createTransformer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.11" data-path="../refguide/expr.html">
            
                <a href="../refguide/expr.html">
            
                    
                    6.11. mobxUtils.expr
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.12" data-path="../refguide/api.html">
            
                <a href="../refguide/api.html">
            
                    
                    6.12. complete api overview
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <a target="_blank" href="https://github.com/mobxjs/awesome-mobx#examples">
            
                    
                    7. 博客、 视频及相关项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" >
            
                <span>
            
                    
                    8. 贴士与技巧
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../faq/faq.html">
            
                <a href="../faq/faq.html">
            
                    
                    8.1. 常见问题解答
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="pitfalls.html">
            
                <a href="pitfalls.html">
            
                    
                    8.2. 常见陷阱与最佳实践
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="decorators.html">
            
                <a href="decorators.html">
            
                    
                    8.3. 如何(不)使用装饰器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="trace.html">
            
                <a href="trace.html">
            
                    
                    8.4. 使用 trace 进行调试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="store.html">
            
                <a href="store.html">
            
                    
                    8.5. 定义数据存储
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="react-performance.html">
            
                <a href="react-performance.html">
            
                    
                    8.6. 优化 React 组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="devtools.html">
            
                <a href="devtools.html">
            
                    
                    8.7. 开发工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="../refguide/spy.html">
            
                <a href="../refguide/spy.html">
            
                    
                    8.8. spy
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../donating.html">
            
                <a href="../donating.html">
            
                    
                    9. 捐赠
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >5.2. async actions & flows</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7F16;&#x5199;&#x5F02;&#x6B65;-actions-&#x52A8;&#x4F5C;"><a name="&#x7F16;&#x5199;&#x5F02;&#x6B65;-actions-&#x52A8;&#x4F5C;" class="plugin-anchor" href="#&#x7F16;&#x5199;&#x5F02;&#x6B65;-actions-&#x52A8;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7F16;&#x5199;&#x5F02;&#x6B65; Actions (&#x52A8;&#x4F5C;)</h1>
<p><code>action</code> &#x5305;&#x88C5;/&#x88C5;&#x9970;&#x5668;&#x53EA;&#x4F1A;&#x5BF9;&#x5F53;&#x524D;&#x8FD0;&#x884C;&#x7684;&#x51FD;&#x6570;&#x4F5C;&#x51FA;&#x53CD;&#x5E94;&#xFF0C;&#x800C;&#x4E0D;&#x4F1A;&#x5BF9;&#x5F53;&#x524D;&#x8FD0;&#x884C;&#x51FD;&#x6570;&#x6240;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#xFF08;&#x4E0D;&#x5305;&#x542B;&#x5728;&#x5F53;&#x524D;&#x51FD;&#x6570;&#x4E4B;&#x5185;&#xFF09;&#x4F5C;&#x51FA;&#x53CD;&#x5E94;&#xFF01;
&#x8FD9;&#x610F;&#x5473;&#x7740;&#x5982;&#x679C; action &#x4E2D;&#x5B58;&#x5728; <code>setTimeout</code>&#x3001;promise &#x7684; <code>then</code> &#x6216; <code>async</code> &#x8BED;&#x53E5;&#xFF0C;&#x5E76;&#x4E14;&#x5728;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4E2D;&#x67D0;&#x4E9B;&#x72B6;&#x6001;&#x6539;&#x53D8;&#x4E86;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E9B;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4E5F;&#x5E94;&#x8BE5;&#x5305;&#x88C5;&#x5728; <code>action</code> &#x4E2D;&#x3002;&#x521B;&#x5EFA;&#x5F02;&#x6B65; action &#x6709;&#x51E0;&#x79CD;&#x65B9;&#x5F0F;&#x3002;&#x4E0D;&#x80FD;&#x8BF4;&#x67D0;&#x79CD;&#x65B9;&#x5F0F;&#x4E00;&#x5B9A;&#x6BD4;&#x5176;&#x4ED6;&#x7684;&#x597D;&#xFF0C;&#x672C;&#x7AE0;&#x53EA;&#x662F;&#x5217;&#x51FA;&#x7F16;&#x5199;&#x5F02;&#x6B65;&#x4EE3;&#x7801;&#x7684;&#x51E0;&#x79CD;&#x4E0D;&#x540C;&#x65B9;&#x5F0F;&#x800C;&#x5DF2;&#x3002;
&#x6211;&#x4EEC;&#x5148;&#x4ECE;&#x4E00;&#x4E2A;&#x57FA;&#x7840;&#x7684;&#x793A;&#x4F8B;&#x5F00;&#x59CB;:</p>
<h3 id="promises"><a name="promises" class="plugin-anchor" href="#promises"><i class="fa fa-link" aria-hidden="true"></i></a>Promises</h3>
<pre><code class="lang-javascript">mobx.configure({ enforceActions: <span class="hljs-literal">true</span> }) <span class="hljs-comment">// &#x4E0D;&#x5141;&#x8BB8;&#x5728;&#x52A8;&#x4F5C;&#x4E4B;&#x5916;&#x8FDB;&#x884C;&#x72B6;&#x6001;&#x4FEE;&#x6539;</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Store</span> </span>{
    @observable githubProjects = []
    @observable state = <span class="hljs-string">&quot;pending&quot;</span> <span class="hljs-comment">// &quot;pending&quot; / &quot;done&quot; / &quot;error&quot;</span>

    @action
    fetchProjects() {
        <span class="hljs-keyword">this</span>.githubProjects = []
        <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;pending&quot;</span>
        fetchGithubProjectsSomehow().then(
            projects =&gt; {
                <span class="hljs-keyword">const</span> filteredProjects = somePreprocessing(projects)
                <span class="hljs-keyword">this</span>.githubProjects = filteredProjects
                <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;done&quot;</span>
            },
            error =&gt; {
                <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;error&quot;</span>
            }
        )
    }
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4F1A;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#xFF0C;&#x56E0;&#x4E3A;&#x4F20;&#x7ED9; <code>fetchGithubProjectsSomehow</code> promise &#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4E0D;&#x662F; <code>fetchProjects</code> &#x52A8;&#x4F5C;&#x7684;&#x4E00;&#x90E8;&#x5206;&#xFF0C;&#x56E0;&#x4E3A;&#x52A8;&#x4F5C;&#x53EA;&#x4F1A;&#x5E94;&#x7528;&#x4E8E;&#x5F53;&#x524D;&#x6808;&#x3002;</p>
<p>&#x9996;&#x9009;&#x7684;&#x7B80;&#x5355;&#x4FEE;&#x590D;&#x662F;&#x5C06;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x53D8;&#x6210;&#x52A8;&#x4F5C;&#x3002;(&#x6CE8;&#x610F;&#x4F7F;&#x7528; <code>action.bound</code> &#x7ED1;&#x5B9A;&#x5728;&#x8FD9;&#x5F88;&#x91CD;&#x8981;&#xFF0C;&#x4EE5;&#x83B7;&#x53D6;&#x6B63;&#x786E;&#x7684; <code>this</code>!):</p>
<pre><code class="lang-javascript">mobx.configure({ enforceActions: <span class="hljs-literal">true</span> })

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Store</span> </span>{
    @observable githubProjects = []
    @observable state = <span class="hljs-string">&quot;pending&quot;</span> <span class="hljs-comment">// &quot;pending&quot; / &quot;done&quot; / &quot;error&quot;</span>

    @action
    fetchProjects() {
        <span class="hljs-keyword">this</span>.githubProjects = []
        <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;pending&quot;</span>
        fetchGithubProjectsSomehow().then(<span class="hljs-keyword">this</span>.fetchProjectsSuccess, <span class="hljs-keyword">this</span>.fetchProjectsError)

    }

    @action.bound
    fetchProjectsSuccess(projects) {
        <span class="hljs-keyword">const</span> filteredProjects = somePreprocessing(projects)
        <span class="hljs-keyword">this</span>.githubProjects = filteredProjects
        <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;done&quot;</span>
    }
    @action.bound
        fetchProjectsError(error) {
            <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;error&quot;</span>
        }
    }
</code></pre>
<p>&#x5C3D;&#x7BA1;&#x8FD9;&#x5F88;&#x6574;&#x6D01;&#x6E05;&#x695A;&#xFF0C;&#x4F46;&#x5F02;&#x6B65;&#x6D41;&#x7A0B;&#x590D;&#x6742;&#x540E;&#x53EF;&#x80FD;&#x4F1A;&#x7565;&#x663E;&#x5570;&#x55E6;&#x3002;&#x53E6;&#x5916;&#x4E00;&#x79CD;&#x65B9;&#x6848;&#x662F;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>action</code> &#x5173;&#x952E;&#x5B57;&#x6765;&#x5305;&#x88C5; promises &#x56DE;&#x8C03;&#x51FD;&#x6570;&#x3002;&#x63A8;&#x8350;&#x8FD9;&#x4E48;&#x505A;&#xFF0C;&#x4F46;&#x4E0D;&#x662F;&#x5F3A;&#x5236;&#x7684;&#xFF0C;&#x8FD8;&#x9700;&#x8981;&#x7ED9;&#x5B83;&#x4EEC;&#x547D;&#x540D;:</p>
<pre><code class="lang-javascript">mobx.configure({ enforceActions: <span class="hljs-literal">true</span> })

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Store</span> </span>{
    @observable githubProjects = []
    @observable state = <span class="hljs-string">&quot;pending&quot;</span> <span class="hljs-comment">// &quot;pending&quot; / &quot;done&quot; / &quot;error&quot;</span>

    @action
    fetchProjects() {
        <span class="hljs-keyword">this</span>.githubProjects = []
        <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;pending&quot;</span>
        fetchGithubProjectsSomehow().then(
            <span class="hljs-comment">// &#x5185;&#x8054;&#x521B;&#x5EFA;&#x7684;&#x52A8;&#x4F5C;</span>
            action(<span class="hljs-string">&quot;fetchSuccess&quot;</span>, projects =&gt; {
                <span class="hljs-keyword">const</span> filteredProjects = somePreprocessing(projects)
                <span class="hljs-keyword">this</span>.githubProjects = filteredProjects
                <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;done&quot;</span>
            }),
            <span class="hljs-comment">// &#x5185;&#x8054;&#x521B;&#x5EFA;&#x7684;&#x52A8;&#x4F5C;</span>
            action(<span class="hljs-string">&quot;fetchError&quot;</span>, error =&gt; {
                <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;error&quot;</span>
            })
        )
    }
}
</code></pre>
<h3 id="runinaction-&#x5DE5;&#x5177;&#x51FD;&#x6570;"><a name="runinaction-&#x5DE5;&#x5177;&#x51FD;&#x6570;" class="plugin-anchor" href="#runinaction-&#x5DE5;&#x5177;&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a><code>runInAction</code> &#x5DE5;&#x5177;&#x51FD;&#x6570;</h3>
<p>&#x5185;&#x8054;&#x52A8;&#x4F5C;&#x7684;&#x7F3A;&#x70B9;&#x662F; TypeScript &#x65E0;&#x6CD5;&#x5BF9;&#x5176;&#x8FDB;&#x884C;&#x7C7B;&#x578B;&#x63A8;&#x5BFC;&#xFF0C;&#x6240;&#x4EE5;&#x4F60;&#x5E94;&#x8BE5;&#x4E3A;&#x6240;&#x6709;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x7C7B;&#x578B;&#x3002;
&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x53EA;&#x5728;&#x52A8;&#x4F5C;&#x4E2D;&#x8FD0;&#x884C;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4E2D;&#x72B6;&#x6001;&#x4FEE;&#x6539;&#x7684;&#x90E8;&#x5206;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x4E3A;&#x6574;&#x4E2A;&#x56DE;&#x8C03;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x52A8;&#x4F5C;&#x3002;
&#x8FD9;&#x79CD;&#x6A21;&#x5F0F;&#x7684;&#x4F18;&#x52BF;&#x662F;&#x5B83;&#x9F13;&#x52B1;&#x4F60;&#x4E0D;&#x8981;&#x5230;&#x5904;&#x5199; <code>action</code>&#xFF0C;&#x800C;&#x662F;&#x5728;&#x6574;&#x4E2A;&#x8FC7;&#x7A0B;&#x7ED3;&#x675F;&#x65F6;&#x5C3D;&#x53EF;&#x80FD;&#x591A;&#x5730;&#x5BF9;&#x6240;&#x6709;&#x72B6;&#x6001;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#xFF1A;</p>
<pre><code class="lang-javascript">mobx.configure({ enforceActions: <span class="hljs-literal">true</span> })

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Store</span> </span>{
    @observable githubProjects = []
    @observable state = <span class="hljs-string">&quot;pending&quot;</span> <span class="hljs-comment">// &quot;pending&quot; / &quot;done&quot; / &quot;error&quot;</span>

    @action
    fetchProjects() {
        <span class="hljs-keyword">this</span>.githubProjects = []
        <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;pending&quot;</span>
        fetchGithubProjectsSomehow().then(
            projects =&gt; {
                <span class="hljs-keyword">const</span> filteredProjects = somePreprocessing(projects)
                <span class="hljs-comment">// &#x5C06;&#x2018;&#x201C;&#x6700;&#x7EC8;&#x7684;&#x201D;&#x4FEE;&#x6539;&#x653E;&#x5165;&#x4E00;&#x4E2A;&#x5F02;&#x6B65;&#x52A8;&#x4F5C;&#x4E2D;</span>
                runInAction(() =&gt; {
                    <span class="hljs-keyword">this</span>.githubProjects = filteredProjects
                    <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;done&quot;</span>
                })
            },
            error =&gt; {
                <span class="hljs-comment">// &#x8FC7;&#x7A0B;&#x7684;&#x53E6;&#x4E00;&#x4E2A;&#x7ED3;&#x5C40;:...</span>
                runInAction(() =&gt; {
                    <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;error&quot;</span>
                })
            }
        )
    }
}
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;<code>runInAction</code> &#x8FD8;&#x53EF;&#x4EE5;&#x7ED9;&#x5B9A;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x4F5C;&#x4E3A;&#x540D;&#x79F0;&#x3002;<code>runInAction(f)</code> &#x5B9E;&#x9645;&#x4E0A;&#x662F; <code>action(f)()</code> &#x7684;&#x8BED;&#x6CD5;&#x7CD6;&#x3002;</p>
<h3 id="async--await"><a name="async--await" class="plugin-anchor" href="#async--await"><i class="fa fa-link" aria-hidden="true"></i></a>async / await</h3>
<p>&#x57FA;&#x4E8E; async / await &#x7684;&#x51FD;&#x6570;&#x5F53;&#x5F00;&#x59CB;&#x4F7F;&#x7528;&#x52A8;&#x4F5C;&#x65F6;&#x8D77;&#x521D;&#x4F3C;&#x4E4E;&#x4F1A;&#x4EE4;&#x4EBA;&#x611F;&#x5230;&#x56F0;&#x60D1;&#x3002;
&#x56E0;&#x4E3A;&#x5728;&#x8BCD;&#x6CD5;&#x4E0A;&#x5B83;&#x4EEC;&#x770B;&#x8D77;&#x6765;&#x662F;&#x540C;&#x6B65;&#x51FD;&#x6570;&#xFF0C;&#x5B83;&#x7ED9;&#x4EBA;&#x7684;&#x5370;&#x8C61;&#x662F; <code>@action</code> &#x5E94;&#x7528;&#x4E8E;&#x6574;&#x4E2A;&#x51FD;&#x6570;&#x3002;
&#x4F46;&#x4E8B;&#x5B9E;&#x5E76;&#x975E;&#x82E5;&#x6B64;&#xFF0C;&#x56E0;&#x4E3A; async / await &#x53EA;&#x662F;&#x56F4;&#x7ED5;&#x57FA;&#x4E8E; promise &#x8FC7;&#x7A0B;&#x7684;&#x8BED;&#x6CD5;&#x7CD6;&#x3002;
&#x7ED3;&#x679C;&#x662F; <code>@action</code> &#x4EC5;&#x5E94;&#x7528;&#x4E8E;&#x4EE3;&#x7801;&#x5757;&#xFF0C;&#x76F4;&#x5230;&#x7B2C;&#x4E00;&#x4E2A; <code>await</code> &#x3002;
&#x5728;&#x6BCF;&#x4E2A; <code>await</code> &#x4E4B;&#x540E;&#xFF0C;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5F02;&#x6B65;&#x51FD;&#x6570;&#x5C06;&#x542F;&#x52A8;&#xFF0C;&#x6240;&#x4EE5;&#x5728;&#x6BCF;&#x4E2A; <code>await</code> &#x4E4B;&#x540E;&#xFF0C;&#x72B6;&#x6001;&#x4FEE;&#x6539;&#x4EE3;&#x7801;&#x5E94;&#x8BE5;&#x88AB;&#x5305;&#x88C5;&#x6210;&#x52A8;&#x4F5C;&#x3002;
&#x8FD9;&#x6B63;&#x662F; <code>runInAction</code> &#x518D;&#x6B21;&#x6D3E;&#x4E0A;&#x7528;&#x573A;&#x7684;&#x5730;&#x65B9;:</p>
<pre><code class="lang-javascript">mobx.configure({ enforceActions: <span class="hljs-literal">true</span> })

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Store</span> </span>{
    @observable githubProjects = []
    @observable state = <span class="hljs-string">&quot;pending&quot;</span> <span class="hljs-comment">// &quot;pending&quot; / &quot;done&quot; / &quot;error&quot;</span>

    @action
    <span class="hljs-keyword">async</span> fetchProjects() {
        <span class="hljs-keyword">this</span>.githubProjects = []
        <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;pending&quot;</span>
        <span class="hljs-keyword">try</span> {
            <span class="hljs-keyword">const</span> projects = <span class="hljs-keyword">await</span> fetchGithubProjectsSomehow()
            <span class="hljs-keyword">const</span> filteredProjects = somePreprocessing(projects)
            <span class="hljs-comment">// await &#x4E4B;&#x540E;&#xFF0C;&#x518D;&#x6B21;&#x4FEE;&#x6539;&#x72B6;&#x6001;&#x9700;&#x8981;&#x52A8;&#x4F5C;:</span>
            runInAction(() =&gt; {
                <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;done&quot;</span>
                <span class="hljs-keyword">this</span>.githubProjects = filteredProjects
            })
        } <span class="hljs-keyword">catch</span> (error) {
            runInAction(() =&gt; {
                <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;error&quot;</span>
            })
        }
    }
}
</code></pre>
<h3 id="flows"><a name="flows" class="plugin-anchor" href="#flows"><i class="fa fa-link" aria-hidden="true"></i></a>flows</h3>
<p>&#x7136;&#x800C;&#xFF0C;&#x66F4;&#x597D;&#x7684;&#x65B9;&#x5F0F;&#x662F;&#x4F7F;&#x7528; <code>flow</code> &#x7684;&#x5185;&#x7F6E;&#x6982;&#x5FF5;&#x3002;&#x5B83;&#x4EEC;&#x4F7F;&#x7528;&#x751F;&#x6210;&#x5668;&#x3002;&#x4E00;&#x5F00;&#x59CB;&#x53EF;&#x80FD;&#x770B;&#x8D77;&#x6765;&#x5F88;&#x4E0D;&#x9002;&#x5E94;&#xFF0C;&#x4F46;&#x5B83;&#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;&#x4E0E; <code>async</code> / <code>await</code> &#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;&#x53EA;&#x662F;&#x4F7F;&#x7528; <code>function *</code> &#x6765;&#x4EE3;&#x66FF; <code>async</code>&#xFF0C;&#x4F7F;&#x7528; <code>yield</code> &#x4EE3;&#x66FF; <code>await</code> &#x3002;
&#x4F7F;&#x7528; <code>flow</code> &#x7684;&#x4F18;&#x70B9;&#x662F;&#x5B83;&#x5728;&#x8BED;&#x6CD5;&#x4E0A;&#x57FA;&#x672C;&#x4E0E; <code>async</code> / <code>await</code> &#x662F;&#x76F8;&#x540C;&#x7684; (&#x53EA;&#x662F;&#x5173;&#x952E;&#x5B57;&#x4E0D;&#x540C;)&#xFF0C;&#x5E76;&#x4E14;&#x4E0D;&#x9700;&#x8981;&#x624B;&#x52A8;&#x7528; <code>@action</code> &#x6765;&#x5305;&#x88C5;&#x5F02;&#x6B65;&#x4EE3;&#x7801;&#xFF0C;&#x8FD9;&#x6837;&#x4EE3;&#x7801;&#x66F4;&#x7B80;&#x6D01;&#x3002;</p>
<p><code>flow</code> &#x53EA;&#x80FD;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x4F7F;&#x7528;&#xFF0C;&#x4E0D;&#x80FD;&#x4F5C;&#x4E3A;&#x88C5;&#x9970;&#x5668;&#x4F7F;&#x7528;&#x3002;
<code>flow</code> &#x53EF;&#x4EE5;&#x5F88;&#x597D;&#x7684;&#x4E0E; MobX &#x5F00;&#x53D1;&#x8005;&#x5DE5;&#x5177;&#x96C6;&#x6210;&#xFF0C;&#x6240;&#x4EE5;&#x5F88;&#x5BB9;&#x6613;&#x8FFD;&#x8E2A; <code>async</code> &#x51FD;&#x6570;&#x7684;&#x8FC7;&#x7A0B;&#x3002;</p>
<pre><code class="lang-javascript">mobx.configure({ enforceActions: <span class="hljs-literal">true</span> })

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Store</span> </span>{
    @observable githubProjects = []
    @observable state = <span class="hljs-string">&quot;pending&quot;</span>

    fetchProjects = flow(<span class="hljs-function"><span class="hljs-keyword">function</span> * (<span class="hljs-params"></span>) </span>{ <span class="hljs-comment">// &lt;- &#x6CE8;&#x610F;*&#x53F7;&#xFF0C;&#x8FD9;&#x662F;&#x751F;&#x6210;&#x5668;&#x51FD;&#x6570;&#xFF01;</span>
        <span class="hljs-keyword">this</span>.githubProjects = []
        <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;pending&quot;</span>
        <span class="hljs-keyword">try</span> {
            <span class="hljs-keyword">const</span> projects = <span class="hljs-keyword">yield</span> fetchGithubProjectsSomehow() <span class="hljs-comment">// &#x7528; yield &#x4EE3;&#x66FF; await</span>
            <span class="hljs-keyword">const</span> filteredProjects = somePreprocessing(projects)
            <span class="hljs-comment">// &#x5F02;&#x6B65;&#x4EE3;&#x7801;&#x5757;&#x4F1A;&#x88AB;&#x81EA;&#x52A8;&#x5305;&#x88C5;&#x6210;&#x52A8;&#x4F5C;&#x5E76;&#x4FEE;&#x6539;&#x72B6;&#x6001;</span>
            <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;done&quot;</span>
            <span class="hljs-keyword">this</span>.githubProjects = filteredProjects
        } <span class="hljs-keyword">catch</span> (error) {
            <span class="hljs-keyword">this</span>.state = <span class="hljs-string">&quot;error&quot;</span>
        }
    })
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../refguide/action.html" class="navigation navigation-prev " aria-label="Previous page: 5.1. action">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../refguide/object-api.html" class="navigation navigation-next " aria-label="Next page: 5.3. Object api">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"5.2. async actions & flows","level":"1.5.2","depth":2,"next":{"title":"5.3. Object api","level":"1.5.3","depth":2,"path":"refguide/object-api.md","ref":"refguide/object-api.md","articles":[]},"previous":{"title":"5.1. action","level":"1.5.1","depth":2,"path":"refguide/action.md","ref":"refguide/action.md","articles":[]},"dir":"ltr"},"config":{"plugins":["github","edit-link","anchors","copy-code-button","github-buttons"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://github.com/SangKa/mobx-docs-cn"},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"github-buttons":{"buttons":[{"user":"SangKa","repo":"MobX-Docs-CN","type":"star","size":"small","count":true},{"user":"SangKa","repo":"MobX-Docs-CN","type":"fork","size":"small","count":true}]},"copy-code-button":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"edit-link":{"label":"完善文档","base":"https://github.com/SangKa/mobx-docs-cn/tree/master/docs"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"MobX 中文文档","gitbook":"3.2.2"},"file":{"path":"best/actions.md","mtime":"2018-06-19T13:53:23.303Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2018-09-18T13:06:52.064Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

